﻿using Microsoft.Extensions.Configuration;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace MuXue.Zyiz.Template.Common.RabbitMQ
{
    public class RabbitMQHelper
    {
        public static IConfigurationSection _config;
        private static ConnectionFactory _factory = null;
        private static IConnection _connection = null;

        public static IServiceProvider ServiceProvider { get; set; }

        private static ConnectionFactory factory
        {
            get
            {
                if (_factory != null) return _factory;
                _factory = new ConnectionFactory();
                _factory.HostName = _config["HostName"];
                _factory.UserName = _config["UserName"];
                _factory.Password = _config["Password"];
                _factory.VirtualHost = _config["VirtualHost"];
                return _factory;
            }
        }
        private static IConnection connection
        {
            get
            {
                if (_connection != null) return _connection;
                _connection = factory.CreateConnection();
                return _connection;
            }
        }

        private static string _customer = string.Empty;

        /// <summary> 
        /// 消息持久化  公平分发
        /// </summary>
        /// <param name="exchange">指定到交换器</param>
        /// <param name="routingKey">路由key</param>
        /// <param name="message">消息内容</param>
        /// <param name="latetime">延时时间-秒</param>
        public static void SendDataToExchange(string message, string queueName, int latetimes = 0)
        {
            //string exchange = string.Concat(_config["VirtualHost"], "_exchange");
            //string queue = string.Format("{0}_{1}_queue", _config["VirtualHost"], queueName);
            //string routingKey = string.Format("{0}_{1}_routingKey", _config["VirtualHost"], queueName);
            string exchange = _config["CourseScheduleExchange"];
            string queue = _config["CourseScheduleQueueName"];
            string routingKey = _config["CourseScheduleRoutingKey"];
            using (IModel channel = connection.CreateModel())
            {
                channel.ExchangeDeclare(exchange: exchange, type: ExchangeType.Direct,durable:true);
                channel.QueueDeclare(queue: queue, durable: true, exclusive: false, autoDelete: false, arguments: null);
                channel.QueueBind(queue: queue, exchange: exchange, routingKey: routingKey);
                var body = Encoding.UTF8.GetBytes(message);
                //IBasicProperties props = channel.CreateBasicProperties();
                //props.Persistent = true;
                channel.BasicPublish(exchange: exchange, routingKey: routingKey, basicProperties: null, body: body);
            }
            LogHelper.Info(string.Format("sender-normal:（exchange:{0},queue:{1},routingKey:{2}）{3}", exchange, queue, routingKey, message));
        }
        
        /// <summary>
        /// 根据重发次数选择对应的队列
        /// </summary>
        /// <param name="reSendNum"></param>
        /// <returns></returns>
        private static int GetTimes(int reSendNum)
        {
            string TimeList = "60,180,300,600,3600";
            string[] latertime = TimeList.Split(',');
            if (reSendNum <= latertime.Length)
            {
                return Convert.ToInt32(latertime[reSendNum - 1]);
            }
            else
            {
                return Convert.ToInt32(latertime[latertime.Length - 1]);
            }
        }
    }
}